package com.learning.spring.cloud.domain.service.impl;

import com.learning.spring.cloud.domain.model.Order;
import com.learning.spring.cloud.domain.repo.OrderRepo;
import com.learning.spring.cloud.domain.service.OrderDomainService;
import com.learning.spring.cloud.dto.OrderDto;
import com.learning.spring.cloud.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderDomainServiceImpl implements OrderDomainService {
    @Autowired
    private OrderRepo orderRepo;

    @Override
    public OrderDto save(OrderDto orderDto) {
        Order order = OrderMapper.INSTANCE.toOrder(orderDto);
        orderRepo.save(order);
        return getOrderDto(order);
    }

    private static OrderDto getOrderDto(Order order) {
        return OrderMapper.INSTANCE.toDto(order);
    }

    @Override
    public OrderDto findByOrderNo(String orderNo) {
        Order order = orderRepo.findByOrderNo(orderNo);
        return getOrderDto(order);
    }
}
